﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Threading;
using System.Collections.Generic;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class D010_MultiPostQuarantine {
		private IWebDriver driver;
		private const Int32 maxPostsNotQuarantineTest = 3;
		private const Int32 intervalNotQuarantineTest = 2;
		private String forumName;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();

			forumName = Guid.NewGuid().ToString();

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.CreateForum("Test board", "Category 1", forumName, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName);
			driver.LogOff();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion


		[Test]
		public void NewTopicNotQuarantined() {
			List<TopicContainer> elements = new List<TopicContainer> {
												new TopicContainer { Title = String.Format("Multi-post enabled, take 2, post: {0}", 1), Content =  "MULTIPOST bla bla, should show up!", ShouldBeVisible = true },
												new TopicContainer { Title = String.Format("Multi-post enabled, take 2, post: {0}", 2), Content =  "MULTIPOST bla bla, should show up!", ShouldBeVisible = true },
												new TopicContainer { Title = String.Format("Multi-post enabled, take 2, post: {0}", 3), Content =  "MULTIPOST bla bla, should show up!", ShouldBeVisible = true },
												new TopicContainer { Title = String.Format("Multi-post enabled, take 2, post: {0}", 4), Content =  "MULTIPOST bla bla, shouldn't show up!", ShouldBeVisible = false }
											};

			// Log on as administrator!
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);

			// Enable multi-post anti-spam!
			driver.EnableMultiPost(intervalNotQuarantineTest, maxPostsNotQuarantineTest);

			// Disable new user trial
			driver.DisableNewUserTrial();

			driver.LogOff();

			String user = Guid.NewGuid().ToString();

			driver.CreateUser(user, user, user + "@mvcforum.org");
			driver.LogOn(user + "@mvcforum.org", user);

			String title = String.Empty;
			String content = String.Empty;
			foreach (TopicContainer con in elements) {
				// Let's wait the interval and try again!			
				driver.Wait(intervalNotQuarantineTest + 1);

				title = String.Format("{0} {1}", con.Title, DateTime.UtcNow);
				driver.CreateTopic(forumName, title, con.Content);

				//Assert.AreEqual(String.Format("{0}forum/f/1/forum%201", TestHelper.SiteBaseURL), driver.Url.ToLower());

				Assert.AreEqual(con.ShouldBeVisible, driver.PageSource.Contains(title), "The newly posted topic's title was not found on the page");
			}
		}
	}

	public class TopicContainer {
		public String Title;
		public String Content;
		public Boolean ShouldBeVisible;
	}
}